﻿using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
using ZilLion.Core.Log;

namespace ZilLion.Core.Unities.Config
{
    public class ZilLionConfigurationManager : IAppConfigProvider
    {
        private static ZilLionConfigurationManager _zilLionConfigurationManager;

        private readonly IAppConfigProvider _appConfigProvider;

        private ZilLionConfigurationManager()
        {
            var configsetting = ConfigurationManager.AppSettings["ConfigExtension"];
            if (configsetting != null)
            {
                var configs = configsetting.Split(';');//程序集和类名用封号隔开
                try
                {
                    if (configs.Length == 1) //必须有程序集名获取第一个IAppConfigProvider实例
                    {
                        var assemblyname = configs[0];
                        if (assemblyname.IsNotNullOrEmpty())
                        {
                            var assembly = Assembly.Load(assemblyname);
                            var first = assembly.GetTypes()
                                .FirstOrDefault(x => x.GetInterfaces().Any(y => y.Name == "IAppConfigProvider"));

                            if (first != null)
                            {
                                var instance = first.FastNew();
                                _appConfigProvider = (IAppConfigProvider) instance;
                            }
                        }


                        return;
                    }
                    if (configs.Length == 2) //必须有程序集名和类名称
                    {
                        var assemblyname = configs[0];
                        var classname = configs[1];
                        if (assemblyname.IsNotNullOrEmpty())
                        {
                            var assembly = Assembly.Load(assemblyname);
                            var first = assembly.GetTypes().FirstOrDefault(x => x.Name == classname);

                            if (first != null)
                            {
                                var instance = first.FastNew();
                                _appConfigProvider = (IAppConfigProvider) instance;
                            }
                        }
                        return;
                    }
                }
                catch (Exception ex)
                {
                    ZilLionLogManager.GetLogger().Error(ex: ex, tags: "初始化系统配置是出现严重错误");
                }

                _appConfigProvider = new DefaultAppConfigProvider();
            }
            else
            {
                _appConfigProvider = new DefaultAppConfigProvider();
            }
        }

        public string GetAppSetting(string key)
        {
            if (_appConfigProvider != null)
                return _appConfigProvider.GetAppSetting(key);
            return string.Empty;
        }

        public void SaveAppSetting(string key, string value)
        {
            if (_appConfigProvider != null)
                _appConfigProvider.SaveAppSetting(key, value);
        }

        public static ZilLionConfigurationManager GetInstance()
        {
            if (_zilLionConfigurationManager == null)
                _zilLionConfigurationManager = new ZilLionConfigurationManager();
            return _zilLionConfigurationManager;
        }
    }
}